package mt.demo.web.comm;

import mt.demo.web.controllerAspect.CsrfShareDataAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by mt on 2015/7/28.
 * 拦截器，为了产生_csrf,为了将R注解到前台，获取当前登陆用户昵称
 */
@Component
public class CsrfHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    private CsrfShareDataAspect shareDataAspect;

    /**
     * This implementation always returns <code>true</code>.
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }

    /**
     * This implementation is empty.
     */
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
        shareDataAspect.addData(modelAndView, request);
        shareDataAspect.refactorModelAndView(modelAndView);

    }

    /**
     * This implementation is empty.
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
